import java.awt.Component;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Vector;
import javax.swing.JLayeredPane;

class MyJLayeredPane extends JLayeredPane implements MouseWheelListener
{
  private static final long serialVersionUID = 1L;
  private Control control;
  //private MyJPanel controlPanel;
  private Vector<MyJScrollPane> scrollpanes;
  
  public MyJLayeredPane()
  {
	System.out.println("MyJLayeredPane, MyJLayeredPane()");
    setName("MyLayeredPane");
    scrollpanes = new Vector<MyJScrollPane>();
    addMouseWheelListener(this);
  }

  public void setControl(Control control)
  {
	System.out.println("MyJLayeredPane, setControl()");
    this.control = control;
    //    controlPanel = new MyJPanel(this);
    //add(controlPanel, JLayeredPane.DEFAULT_LAYER);
  }

  public Control getControl()
  {
	System.out.println("MyJLayeredPane, getControl()");
    return this.control;
  }

  public MyJTextPane getTextPane(int id)
  {
	System.out.println("MyJLayeredPane, getTextPane(int id)");
	MyJTextPane textpane = scrollpanes.get(id).getTextPane();
    return textpane;
  }
  
  public void addTextPane()
  {
	  System.out.println("MyJLayeredPane, addTextPane()");
	  MyJTextPane textpane = new MyJTextPane();
	  MyJScrollPane scrollpane = new MyJScrollPane(textpane , scrollpanes.size()+1);
	  scrollpane.setBounds(1, 41, 793, 410);
	  scrollpane.addMouseWheelListener(this);
	  scrollpane.setViewportView(textpane);
	  scrollpanes.add(scrollpane);
	  addInLayer(scrollpanes.size()-1);
  }

  public void addInLayer(int id)
  {
	  System.out.println("MyJLayeredPane, addInLayer(" + id + ")" );
	  add(scrollpanes.get(id), JLayeredPane.DEFAULT_LAYER);
  }
  

  public void removeLayer(int id)
  {
	  System.out.println("MyJLayeredPane, removeLayer(int id)");
	  remove(scrollpanes.get(id));
  }

  public void removeLayers()
  {
	  System.out.println("MyJLayeredPane, removeLayers()");

	  for(MyJScrollPane s : scrollpanes) {
		  removeLayer(s.getId() - 1);
	  }
	  scrollpanes.clear();
	  control.setCurrentId(0);
	  this.repaint();
  }
	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
	    System.out.println("MyLayeredPane.mouseWheelMoved(MouseWheelEvent e)");
	    int rotation = e.getWheelRotation();
	    int countComponents = getComponentCountInLayer(JLayeredPane.DEFAULT_LAYER);
	    Component[] components = getComponentsInLayer(JLayeredPane.DEFAULT_LAYER);
	    if (rotation > 0) {
			moveToBack(components[0]);
		} else {
			moveToFront(components[countComponents-1]);
		}
	    int id = 0;
	    try {
	    	MyJScrollPane pane = (MyJScrollPane) this.getComponent(0);
	    	id = pane.getId() - 1;
	    } catch( Exception ex ) {
	    }
	    control.setCurrentId( id );
    }
	
	  public void finalize() throws Throwable
	  {
		System.out.println("MyJLayeredPane, finalize()");
	  }
  
  public String toString()
  {
	System.out.println("MyJLayeredPane, toString()");
	return super.toString();
  }
}
